{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d2caad9a",
   "metadata": {},
   "source": [
    "## 1. APP 现货行情-历史K线函数总览"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "008eaa0c",
   "metadata": {},
   "source": [
    "|方法|解释|\n",
    "|:---|:---|\n",
    "|get_history_candle|获取产品的历史K线数据|\n",
    "|get_history_candle_latest|获取产品指定数量的最新历史K线数据|\n",
    "|get_history_candle_by_date|获取产品指定日期的历史K线数据|\n",
    "|get_history_candle_latest_ts|获取历史K线数据中最新的毫秒时间戳|\n",
    "|update_history_candle|更新产品历史K线数据到指定时间|\n",
    "|candle_to_df|将Binance的candle转化为DataFrame|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed2f07cc",
   "metadata": {},
   "source": [
    "## 2. 模型实例化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e1c896d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binance_interface.app import BinanceSPOT\n",
    "from binance_interface.app.utils import eprint\n",
    "import paux.date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a7adbd4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转发：需搭建转发服务器，可参考：https://github.com/pyted/binance_resender\n",
    "proxy_host = None\n",
    "key = 'xxxx'\n",
    "secret = 'xxxx'\n",
    "\n",
    "binanceSPOT = BinanceSPOT(\n",
    "    key=key,\n",
    "    secret=secret,\n",
    "    proxy_host=proxy_host,\n",
    "    timezone='Asia/Shanghai',\n",
    ")\n",
    "market = binanceSPOT.market"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "865b4b3a",
   "metadata": {},
   "source": [
    "## 3. 获取产品的历史K线数据 get_history_candle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "59f4dab8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': array([[1.67250240e+12, 1.65901000e+04, 1.65969700e+04, ...,\n",
      "        7.80654600e+01, 1.29542721e+06, 0.00000000e+00],\n",
      "       [1.67250246e+12, 1.65960900e+04, 1.65973100e+04, ...,\n",
      "        4.77967500e+01, 7.93208688e+05, 0.00000000e+00],\n",
      "       [1.67250252e+12, 1.65965800e+04, 1.65977700e+04, ...,\n",
      "        5.80070800e+01, 9.62726268e+05, 0.00000000e+00],\n",
      "       ...,\n",
      "       [1.67258862e+12, 1.65549300e+04, 1.65585000e+04, ...,\n",
      "        4.06363700e+01, 6.72814001e+05, 0.00000000e+00],\n",
      "       [1.67258868e+12, 1.65578200e+04, 1.65599900e+04, ...,\n",
      "        4.00219800e+01, 6.62708243e+05, 0.00000000e+00],\n",
      "       [1.67258874e+12, 1.65589800e+04, 1.65597700e+04, ...,\n",
      "        4.60841800e+01, 7.63086276e+05, 0.00000000e+00]]),\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "candle_result = market.get_history_candle(\n",
    "    symbol='BTCUSDT',\n",
    "    start='2023-01-01 00:00:00',\n",
    "    end='2023-01-01 23:59:00',\n",
    "    bar='1m',\n",
    ")\n",
    "eprint(candle_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75f3ef4d",
   "metadata": {},
   "source": [
    "## 4. 获取产品指定数量的最新历史K线数据 get_history_candle_latest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6e54b67f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': array([[1.70605188e+12, 3.95820100e+04, 3.95983100e+04, ...,\n",
      "        1.11380100e+01, 4.40831314e+05, 0.00000000e+00],\n",
      "       [1.70605194e+12, 3.95983100e+04, 3.96278200e+04, ...,\n",
      "        1.59375200e+01, 6.31265989e+05, 0.00000000e+00],\n",
      "       [1.70605200e+12, 3.96235500e+04, 3.96235500e+04, ...,\n",
      "        3.86567000e+00, 1.53123466e+05, 0.00000000e+00],\n",
      "       ...,\n",
      "       [1.70608770e+12, 4.01000000e+04, 4.01049500e+04, ...,\n",
      "        8.39811000e+00, 3.36733060e+05, 0.00000000e+00],\n",
      "       [1.70608776e+12, 4.00526100e+04, 4.00958300e+04, ...,\n",
      "        1.75785300e+01, 7.04393469e+05, 0.00000000e+00],\n",
      "       [1.70608782e+12, 4.00710000e+04, 4.00958300e+04, ...,\n",
      "        1.23730500e+01, 4.95945541e+05, 0.00000000e+00]]),\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "candle_result = market.get_history_candle_latest(\n",
    "    symbol='BTCUSDT',\n",
    "    length=600,\n",
    "    bar='1m',\n",
    ")\n",
    "eprint(candle_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "256d6d82",
   "metadata": {},
   "source": [
    "## 5. 获取产品指定日期的历史K线数据 get_history_candle_by_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "374e06c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': array([[1.67250240e+12, 1.65901000e+04, 1.65969700e+04, ...,\n",
      "        7.80654600e+01, 1.29542721e+06, 0.00000000e+00],\n",
      "       [1.67250246e+12, 1.65960900e+04, 1.65973100e+04, ...,\n",
      "        4.77967500e+01, 7.93208688e+05, 0.00000000e+00],\n",
      "       [1.67250252e+12, 1.65965800e+04, 1.65977700e+04, ...,\n",
      "        5.80070800e+01, 9.62726268e+05, 0.00000000e+00],\n",
      "       ...,\n",
      "       [1.67258862e+12, 1.65549300e+04, 1.65585000e+04, ...,\n",
      "        4.06363700e+01, 6.72814001e+05, 0.00000000e+00],\n",
      "       [1.67258868e+12, 1.65578200e+04, 1.65599900e+04, ...,\n",
      "        4.00219800e+01, 6.62708243e+05, 0.00000000e+00],\n",
      "       [1.67258874e+12, 1.65589800e+04, 1.65597700e+04, ...,\n",
      "        4.60841800e+01, 7.63086276e+05, 0.00000000e+00]]),\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "candle_result = market.get_history_candle_by_date(\n",
    "    symbol='BTCUSDT',\n",
    "    date='2023-01-01',  # 默认时区为美国时区\n",
    "    bar='1m',\n",
    ")\n",
    "eprint(candle_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39db6edd",
   "metadata": {},
   "source": [
    "## 6. 获取历史K线数据中最新的毫秒时间戳 get_history_candle_latest_ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2871bfdc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200, 'data': 1706087820000.0, 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "ts_result = market.get_history_candle_latest_ts(\n",
    "    bar='1m',\n",
    ")\n",
    "eprint(ts_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71249325",
   "metadata": {},
   "source": [
    "## 7. 更新产品历史K线数据到指定时间 update_history_candle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "94bf82fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取candle1，待更新\n",
    "candle1 = market.get_history_candle(\n",
    "    symbol='BTCUSDT',\n",
    "    start='2023-01-01 10:00:00',\n",
    "    end='2023-01-02 10:00:00',\n",
    "    bar='1m',\n",
    ")['data']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "397c00d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': array([[1.67258880e+12, 1.65587300e+04, 1.65610900e+04, ...,\n",
      "        7.35524600e+01, 1.21800174e+06, 0.00000000e+00],\n",
      "       [1.67258886e+12, 1.65595700e+04, 1.65653700e+04, ...,\n",
      "        7.42564800e+01, 1.22986767e+06, 0.00000000e+00],\n",
      "       [1.67258892e+12, 1.65648400e+04, 1.65846600e+04, ...,\n",
      "        2.65374950e+02, 4.39791119e+06, 0.00000000e+00],\n",
      "       ...,\n",
      "       [1.67267502e+12, 1.67370500e+04, 1.67387700e+04, ...,\n",
      "        2.54655500e+01, 4.26162346e+05, 0.00000000e+00],\n",
      "       [1.67267508e+12, 1.67331500e+04, 1.67342900e+04, ...,\n",
      "        2.68978800e+01, 4.50016189e+05, 0.00000000e+00],\n",
      "       [1.67267514e+12, 1.67301100e+04, 1.67378900e+04, ...,\n",
      "        7.75499100e+01, 1.29767302e+06, 0.00000000e+00]]),\n",
      " 'msg': ''}\n",
      "历史K线时间起点： 2023-01-02 00:00:00\n",
      "历史K线时间终点： 2023-01-02 23:59:00\n",
      "历史K线时间长度： 1440\n"
     ]
    }
   ],
   "source": [
    "# 更新candle1到指定日期时间(智能节约权重)\n",
    "candle_result = market.update_history_candle(\n",
    "    candle=candle1,  # 支持candle1为空\n",
    "    symbol='BTCUSDT',\n",
    "    length=1440,  # 保留数量\n",
    "    end='2023-01-02 23:59:00',  # end默认为本地计算机时间戳\n",
    "    bar='1m',\n",
    ")\n",
    "eprint(candle_result)\n",
    "\n",
    "candle_start = paux.date.to_fmt(\n",
    "    candle_result['data'][0, 0],\n",
    "    timezone='Asia/Shanghai',\n",
    ")\n",
    "\n",
    "candle_end = paux.date.to_fmt(\n",
    "    candle_result['data'][-1, 0],\n",
    "    timezone='Asia/Shanghai',\n",
    ")\n",
    "\n",
    "candle_length = candle_result['data'].shape[0]\n",
    "\n",
    "print('历史K线时间起点：', candle_start)\n",
    "print('历史K线时间终点：', candle_end)\n",
    "print('历史K线时间长度：', candle_length)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "065531ff",
   "metadata": {},
   "source": [
    "## 8. 将Binance的candle转化为DataFrame candle_to_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4b38c9d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.67258880e+12, 1.65587300e+04, 1.65610900e+04, ...,\n",
       "        7.35524600e+01, 1.21800174e+06, 0.00000000e+00],\n",
       "       [1.67258886e+12, 1.65595700e+04, 1.65653700e+04, ...,\n",
       "        7.42564800e+01, 1.22986767e+06, 0.00000000e+00],\n",
       "       [1.67258892e+12, 1.65648400e+04, 1.65846600e+04, ...,\n",
       "        2.65374950e+02, 4.39791119e+06, 0.00000000e+00],\n",
       "       ...,\n",
       "       [1.67267502e+12, 1.67370500e+04, 1.67387700e+04, ...,\n",
       "        2.54655500e+01, 4.26162346e+05, 0.00000000e+00],\n",
       "       [1.67267508e+12, 1.67331500e+04, 1.67342900e+04, ...,\n",
       "        2.68978800e+01, 4.50016189e+05, 0.00000000e+00],\n",
       "       [1.67267514e+12, 1.67301100e+04, 1.67378900e+04, ...,\n",
       "        7.75499100e+01, 1.29767302e+06, 0.00000000e+00]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "candle = candle_result['data']\n",
    "candle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7baa9ca9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>openTs</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>closeTs</th>\n",
       "      <th>turnover</th>\n",
       "      <th>tradeNum</th>\n",
       "      <th>buyVolume</th>\n",
       "      <th>buyTurnover</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-01-02 00:00:00</td>\n",
       "      <td>16558.73</td>\n",
       "      <td>16561.09</td>\n",
       "      <td>16558.00</td>\n",
       "      <td>16559.17</td>\n",
       "      <td>139.80668</td>\n",
       "      <td>2023-01-02 00:00:59</td>\n",
       "      <td>2.315116e+06</td>\n",
       "      <td>4247.0</td>\n",
       "      <td>73.55246</td>\n",
       "      <td>1.218002e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2023-01-02 00:01:00</td>\n",
       "      <td>16559.57</td>\n",
       "      <td>16565.37</td>\n",
       "      <td>16558.45</td>\n",
       "      <td>16565.31</td>\n",
       "      <td>156.18004</td>\n",
       "      <td>2023-01-02 00:01:59</td>\n",
       "      <td>2.586717e+06</td>\n",
       "      <td>3350.0</td>\n",
       "      <td>74.25648</td>\n",
       "      <td>1.229868e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-01-02 00:02:00</td>\n",
       "      <td>16564.84</td>\n",
       "      <td>16584.66</td>\n",
       "      <td>16563.68</td>\n",
       "      <td>16574.35</td>\n",
       "      <td>448.30474</td>\n",
       "      <td>2023-01-02 00:02:59</td>\n",
       "      <td>7.429515e+06</td>\n",
       "      <td>7990.0</td>\n",
       "      <td>265.37495</td>\n",
       "      <td>4.397911e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2023-01-02 00:03:00</td>\n",
       "      <td>16574.35</td>\n",
       "      <td>16574.52</td>\n",
       "      <td>16565.43</td>\n",
       "      <td>16566.26</td>\n",
       "      <td>196.98567</td>\n",
       "      <td>2023-01-02 00:03:59</td>\n",
       "      <td>3.263886e+06</td>\n",
       "      <td>5183.0</td>\n",
       "      <td>104.42095</td>\n",
       "      <td>1.730175e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-01-02 00:04:00</td>\n",
       "      <td>16566.70</td>\n",
       "      <td>16566.70</td>\n",
       "      <td>16562.03</td>\n",
       "      <td>16563.20</td>\n",
       "      <td>96.06997</td>\n",
       "      <td>2023-01-02 00:04:59</td>\n",
       "      <td>1.591356e+06</td>\n",
       "      <td>2601.0</td>\n",
       "      <td>39.03212</td>\n",
       "      <td>6.465452e+05</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                openTs      open      high       low     close     volume  \\\n",
       "0  2023-01-02 00:00:00  16558.73  16561.09  16558.00  16559.17  139.80668   \n",
       "1  2023-01-02 00:01:00  16559.57  16565.37  16558.45  16565.31  156.18004   \n",
       "2  2023-01-02 00:02:00  16564.84  16584.66  16563.68  16574.35  448.30474   \n",
       "3  2023-01-02 00:03:00  16574.35  16574.52  16565.43  16566.26  196.98567   \n",
       "4  2023-01-02 00:04:00  16566.70  16566.70  16562.03  16563.20   96.06997   \n",
       "\n",
       "               closeTs      turnover  tradeNum  buyVolume   buyTurnover  \n",
       "0  2023-01-02 00:00:59  2.315116e+06    4247.0   73.55246  1.218002e+06  \n",
       "1  2023-01-02 00:01:59  2.586717e+06    3350.0   74.25648  1.229868e+06  \n",
       "2  2023-01-02 00:02:59  7.429515e+06    7990.0  265.37495  4.397911e+06  \n",
       "3  2023-01-02 00:03:59  3.263886e+06    5183.0  104.42095  1.730175e+06  \n",
       "4  2023-01-02 00:04:59  1.591356e+06    2601.0   39.03212  6.465452e+05  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = market.candle_to_df(candle)\n",
    "df.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
